/*
 * @Author: silence
 * @Date: 2022-05-18 16:39:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-07-26 15:41:47
 * @Description: VIP 、支付相关接口
 */

import '../../model/AlipayOrderEntity.dart';
import '../../model/VipRulesEntity.dart';
import '../../utils/Tool/MyTools.dart';
import '../ApiUtils.dart';
import '../R.dart';

/// @description: 支付规则
/// device：设置标识 ：0:默认,1:Android,2:IOS
/// @return {*}
Future<R> api_vip_rules({int device = 1}) async {
  Map<String, dynamic> map = Map();
  map['device'] = device;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.postMethod, '/api/v1.pay/common/rules',
      param: map,isShowLog: true);

  R r = R.fromData(methodAwait, handlerData: (map) {
    List<VipRulesEntity> dataList = [];
    if (!MyTools.listIsEmpty(map)) {
      for (var item in map) {
        dataList.add(VipRulesEntity.fromJson(item));
      }
    }
    return dataList;
  });

  return r;
}

/// @description: 去支付创建订单
/// paytype ： 支付类型 [alipay:支付宝, wxpay:微信, iap:苹果支付]
/// rulesid ：充值规则ID
/// tradetype ： 交易类型 [app:APP支付,wap:手机端支付]
/// @return {*}
Future<R> api_vip_pay(
    {required String paytype,
    required String rulesid,
    String tradetype = 'app'}) async {
  Map<String, dynamic> map = Map();
  map['paytype'] = paytype;
  map['rulesid'] = rulesid;
  map['tradetype'] = tradetype;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.postMethod, '/api/v1.pay/user/create',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    // MyEntityLog.log('WechatOrderEntity', map);

    dynamic order;
    if (!MyTools.mapIsEmpty(map)) {
      if (paytype == 'alipay') {
        //order = AlipayOrderEntity.fromJson(map);
      } else {
        // order = WechatOrderEntity.fromJson(map);
      }
    }
    return order;
  });

  return r;
}

Future<R> googlePay(Map<String, dynamic> pay) async {
  String url = "/api/v1.pay/user/google";
  Map<String, dynamic> map = {};
  map['receipt_data'] = pay;
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);
  R r = R.fromData(methodAwait);
  return r;
}
